package lib.relay;

import android.os.Handler;
import android.util.Log;

public class TimerCtrlBase implements Runnable {
	
	public interface TimerRunListener 
	{
        void TimerRun();
	}
	private TimerRunListener m_RunListener = null;
	private static TimerCtrlBase _this = null;
	private static Handler _hTimer = null;
	private long m_period = 0;
	//private boolean isPause = false;
	private Thread m_TimeThread = null;
	
	
	public TimerCtrlBase(TimerRunListener runListener, long period)
	{
		_this = this;
		m_period = period;
		m_RunListener = runListener;
		_hTimer = new Handler();
	}
	
	public void StartTimer()
	{
		_hTimer.postDelayed( _this, m_period);
		//isPause = false;
	}
	
	public void StopTimer()
	{
		_hTimer.removeCallbacks(_this);
		//_hTimer = null;
	}
	
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		/*while (isPause)
		{  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException e) {  
            }     
        }*/ 
		
		if (m_TimeThread!=null)
		{
			try {
				m_TimeThread.interrupt();
				Thread.sleep(100);
				m_TimeThread = null;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		m_TimeThread = new Thread()
		{
			public void run()
			{
				if(m_RunListener!=null)
					m_RunListener.TimerRun();
			}
			
		};
		
		m_TimeThread.start();
		
		_hTimer.postDelayed(this, m_period);
	}

}
